'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async carousecreat() {
    const { ctx } = this;
    const { arouselurl } = ctx.request.body;
    if (!arouselurl) return ctx.body = { success: false, msg: '图片地址不能为空' };
    try {
      const res = await ctx.model.Carousel.create({
        arouselurl,
      });
      ctx.body = { success: true, data: res, msg: '上传成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '发生未知意外' };
    }
  }

  async carousefind() {
    const { ctx } = this;
    try {
      const res = await ctx.model.Carousel.findAll();
      ctx.body = { success: true, data: res };
    } catch (e) {
      ctx.body = { success: false, msg: '发生未知意外' };
    }
  }

  async carousedestory() {
    const { ctx } = this;
    const { carouselid } = ctx.request.body;
    if (!carouselid) return ctx.body = { success: false, msg: '请传入图片id' };
    try {
      await ctx.model.Carousel.destroy({
        where: {
          carouselid,
        },
      });
      ctx.body = { success: true, msg: '删除成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '发生未知意外' };
    }
  }
}

module.exports = HomeController;
